const DateUtil = {};

DateUtil.MINUTE = 1000 * 60;
DateUtil.HOUR = DateUtil.MINUTE * 60;
DateUtil.DAY = DateUtil.HOUR * 24;
DateUtil.WEEK = DateUtil.DAY * 7;
//时间转换到本地的天
DateUtil.toLocaleDateString = function(datestr) {
	if(!datestr || datestr=="")
	{
		return ""
	}
  let date = new Date(datestr);

  let m = date.getMonth() + 1;
  if (m < 10) {
    m = "0" + m;
  }
  let day = date.getDate();
  if (day < 10) {
    day = "0" + day;
  }
  return date.getFullYear() + "-" + m + "-" + day;
};

DateUtil.toLocaleMinutString = function(datestr) {
  let date = new Date(datestr);

  let m = date.getMonth() + 1;
  if (m < 10) {
    m = "0" + m;
  }
  let day = date.getDate();
  if (day < 10) {
    day = "0" + day;
  }

  let h = date.getHours();
  if (h < 10) {
    h = "0" + h;
  }
  let MM = date.getMinutes();
  if (MM < 10) {
    MM = "0" + MM;
  }
  return `${date.getFullYear()}-${m}-${day} ${h}:${MM}`;
};

//时间转换成显示时间 如比较进的显示多少分钟前 这种
DateUtil.toShowTime = function(datestr) {
  let curdate = new Date();
  let date = new Date(datestr);
  let dt = curdate.getTime() - date.getTime();
  if (dt < DateUtil.HOUR) {
    let MINUTE = dt / DateUtil.MINUTE;
    MINUTE = Math.max(MINUTE, 1);
    MINUTE = Math.floor(MINUTE);
    return MINUTE + "分钟前";
  }
  if (dt < DateUtil.DAY) {
    let HOUR = dt / DateUtil.HOUR;
    HOUR = Math.max(HOUR, 1);
    HOUR = Math.floor(HOUR);
    return HOUR + "小时前";
  }
  if (dt < DateUtil.WEEK) {
    let DAY = dt / DateUtil.DAY;
    DAY = Math.max(DAY, 1);
    DAY = Math.floor(DAY);
    return DAY + "天前";
  }
  return DateUtil.toLocaleDateString(datestr);
};

//计算两个时间差
DateUtil.timeDifference = function(datestr) {
  //如果时间格式是正确的，那下面这一步转化时间格式就可以不用了
      var dateEnd = new Date(datestr);//将-转化为/，使用new Date
      var dateBegin = new Date();//获取当前时间
      var dateDiff = dateEnd.getTime() - dateBegin.getTime();//时间差的毫秒数
      var dayDiff = Math.floor(dateDiff / (24 * 3600 * 1000));//计算出相差天数
      var leave1=dateDiff%(24*3600*1000)    //计算天数后剩余的毫秒数
      var hours=Math.floor(leave1/(3600*1000))//计算出小时数
      //计算相差分钟数
      var leave2=leave1%(3600*1000)    //计算小时数后剩余的毫秒数
      var minutes=Math.floor(leave2/(60*1000))//计算相差分钟数
      //计算相差秒数
      var leave3=leave2%(60*1000)      //计算分钟数后剩余的毫秒数
      var seconds=Math.round(leave3/1000)
	  
	  var allhours = Math.floor(dateDiff / (3600 * 1000 ));//小时
	  // return (dayDiff+"天"+hours+"小时"+minutes+"分钟"+seconds+"秒")
	  if(allhours>0){
		return allhours+"小时"  
	  }else if(minutes > 0 ){
		 return minutes+"分钟"
	  }else{
		  return minutes+5+"分钟内自动通过" //5分钟巡检自动审阅
	  }
	  
	  
      // console.log(" 相差 "+dayDiff+"天 "+hours+"小时 "+minutes+" 分钟"+seconds+" 秒")
      // console.log(dateDiff+"时间差的毫秒数",dayDiff+"计算出相差天数",leave1+"计算天数后剩余的毫秒数"
      //     ,hours+"计算出小时数",minutes+"计算相差分钟数",seconds+"计算相差秒数");
};
export default DateUtil;
